Skip to content

fix(schedule): attribute dispatched tasks and runs to their schedule execution - #1187

Open
wcy12378 wants to merge 1 commit into
Tencent:mainfrom
wcy12378:codex/schedule-execution-attribution
Open

wcy12378 wants to merge 1 commit into
Tencent:mainfrom
wcy12378:codex/schedule-execution-attribution

Conversation

@wcy12378

Copy link
Copy Markdown

fix(schedule): attribute dispatched tasks and runs to their schedule execution

This is a further backend slice of #460 (schedule execution ledger / outcome attribution). It persists the specific schedule execution ID on each dispatched Task and Run, retains it through child SOPs and telemetry/log ingestion, and keeps the existing per-Run TraceID == Run.ID contract unchanged. It does not close #460.

Problem

The scheduled dispatch path currently propagates only the schedule job ID (scheduleJobId) into the Task and Run context. A job that runs repeatedly produces one execution per occurrence, but the job ID cannot distinguish them — so a Task, Run, child SOP, or worker log cannot be tied back to the specific ScheduleJobExecution that produced it.

Change

Add an optional, server-derived scheduleExecutionId (sjx-<trace>, via domain.ScheduleJobExecutionID(job.LastTraceID)) that flows from the scheduler-side dispatch into the Task and Run execution context:

  • internal/domain/task.go — Task.ScheduleExecutionID (scheduleExecutionId,omitempty).
  • internal/domain/observability.go — TelemetryContext.ScheduleExecutionID (scheduleExecutionId,omitempty).
  • internal/httpapi/schedule.go — dispatchScheduledRun derives the ID from the claimed execution's job.LastTraceID. An empty trace (a direct dispatch without a claimed execution) never fabricates an sjx- ID.
  • internal/httpapi/dispatch.go — dispatchRequest carries the server-derived ID; dispatchIssue writes it onto the Task before ReserveIssueDispatch, and into the Run's TelemetryContext. Public manual dispatch (POST issue/room dispatch) never sets it, so callers cannot inject attribution.
  • internal/engine/run_events.go — worker-log ingestion unconditionally overwrites ScheduleExecutionID from the persisted Run context, including clearing it to empty for manual runs. Worker-reported values are never trusted.
  • docs/api/openapi.yaml — documents the read-only Task.scheduleExecutionId.

Child SOPs inherit the field through the existing ExecutionContext value copy (engine/sop_node.go), and observation context is rebuilt from the persisted Run context, so both retain the attribution. No new tables, no SQL migration, no data backfill; old records keep an empty value.

Verification (Linux/amd64, Docker python:3.12, real schedule/engine paths)

  • New RED→GREEN tests (kept from the pre-implementation RED run):
    • internal/httpapi/schedule_execution_attribution_test.go — TestScheduleExecutionAttributionSeparatesOccurrences: two real RunNow occurrences on different issues produce distinct execution IDs on Task and Run JSON, the first occurrence is not overwritten by the second, and manual/legacy dispatch stay empty. PASS.
    • internal/engine/schedule_execution_context_test.go — TestScheduledExecutionContextSurvivesChildSOP (parent→child inheritance, TraceID == Run.ID) and TestScheduledExecutionContextOverridesWorkerClaims (forged worker telemetry/log overwritten by the persisted Run value, including the empty/manual case). PASS.
  • go vet (linux target) on the changed packages: clean.
  • docs/api/openapi.yaml parses; the new field is present and readOnly.
  • Full-package regression for domain and schedule: PASS. engine/httpapi full suites show only pre-existing, environment-specific failures, all reproduced on the unmodified baseline (timing-sensitive lease/cancel/recovery tests under parallel load; a pre-existing nil panic in an assistant-diagnostics test; a runner source-build test needing a Go toolchain). Not introduced by this slice.

Scope / not covered (by design)

Related

Closes part of #460 (backend attribution slice only); does not close the whole issue.

…execution

Add a server-derived scheduleExecutionId (sjx-<trace>) to the scheduled
dispatch path so each dispatched Task and Run, its child SOPs, and ingested
telemetry/log carry the specific ScheduleJobExecution that produced them.

- dispatchScheduledRun derives the ID from the claimed execution trace;
  an empty trace never fabricates an sjx- ID.
- dispatchIssue writes it onto the Task before ReserveIssueDispatch and
  into the Run TelemetryContext; public manual dispatch never sets it.
- worker-log ingestion unconditionally overwrites the value from the
  persisted Run context (including clearing it for manual runs).
- child SOPs inherit it via the existing ExecutionContext value copy;
  TraceID == Run.ID is unchanged. No new tables, no migration, no backfill.
- document the read-only Task.scheduleExecutionId in the OpenAPI schema.

This is a backend slice of Tencent#460 only; it does not close the issue. Related
to Tencent#460. Does not duplicate Tencent#1128 or Tencent#1145.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] 完善现有定时任务的执行历史、成果归因与统计查询

1 participant